package com.gzy.android_douban.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.gzy.android_douban.data.api.ApiService;
import com.gzy.android_douban.data.model.MovieDateilModel;
import com.gzy.android_douban.data.model.MovieModel;
import com.gzy.android_douban.databinding.FragmentHomeBinding;
import com.gzy.android_douban.ui.BaseFragment;
import com.gzy.network.NetworkApi;
import com.gzy.network.observer.BaseObserver;


public class HomeFragment extends BaseFragment<FragmentHomeBinding> {
  FragmentHomeBinding binding;

  @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    initClick();

  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//    return super.onCreateView(inflater, container, savedInstanceState);
    binding = FragmentHomeBinding.inflate(inflater, container,false);
    View view = binding.getRoot();

    initData();

    return view;
  }

  private void initData() {
    getMovieList(0,10);
    getMovieDateil();
    getMovieListByRating(0,10);
  }

  private void getMovieListByRating(Integer currentPage,Integer pageSize) {
    NetworkApi.createService(ApiService.class)
            .getMovieListByRatingApi(currentPage,pageSize,"thisisatoken.123.tokenend")
            .compose(NetworkApi.applySchedulers(new BaseObserver<MovieModel>() {

              @Override
              public void onSuccess(MovieModel movieModel) {
                System.out.println("请求成功getMovieListByRating");
                System.out.println(movieModel);
              }

              @Override
              public void onFailure(Throwable e) {
                System.out.println("请求失败");
//                if (e.getMessage().contains("401")) {
//                  AppContext.showToast("用户名密码错误");
//                }
//                if (e.getMessage().contains("400")) {
//                  AppContext.showToast("验证码输入错误");
//                }
//                KLog.e(TAG, e.getMessage());
              }
            }));
  }

  private void getMovieDateil() {
    NetworkApi.createService(ApiService.class)
            .getMovieDetailApi(10007,"thisisatoken.123.tokenend")
            .compose(NetworkApi.applySchedulers(new BaseObserver<MovieDateilModel>() {

              @Override
              public void onSuccess(MovieDateilModel movieDateilModel) {
                System.out.println("请求成功getMovieDateil");
                System.out.println(movieDateilModel);
              }

              @Override
              public void onFailure(Throwable e) {
                System.out.println("请求失败");
//                if (e.getMessage().contains("401")) {
//                  AppContext.showToast("用户名密码错误");
//                }
//                if (e.getMessage().contains("400")) {
//                  AppContext.showToast("验证码输入错误");
//                }
//                KLog.e(TAG, e.getMessage());
              }
            }));
  }

  public void getMovieList(Integer currentPage,Integer pageSize) {
    NetworkApi.createService(ApiService.class)
            .getMovieListApi(currentPage,pageSize,
                    null,
                    "喜剧",
                    null,
                    "thisisatoken.123.tokenend")
            .compose(NetworkApi.applySchedulers(new BaseObserver<MovieModel>() {
              @Override
              public void onSuccess(MovieModel movieModel) {
                System.out.println("请求成功getMovieList");
                System.out.println(movieModel);
              }

              @Override
              public void onFailure(Throwable e) {
                System.out.println("请求失败");
//                if (e.getMessage().contains("401")) {
//                  AppContext.showToast("用户名密码错误");
//                }
//                if (e.getMessage().contains("400")) {
//                  AppContext.showToast("验证码输入错误");
//                }
//                KLog.e(TAG, e.getMessage());
              }
            }));
  }



  private void initClick() {
    binding.tvTuijian.getPaint().setFakeBoldText(true);
    binding.tvTuijian.setTextSize(24);
    binding.tvTuijian.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        binding.tvTuijian.getPaint().setFakeBoldText(true);
        binding.tvTuijian.setTextSize(24);
        binding.tvGuangchang.getPaint().setFakeBoldText(false);
        binding.tvGuangchang.setTextSize(20);
        binding.tvRebang.getPaint().setFakeBoldText(false);
        binding.tvRebang.setTextSize(20);
      }
    });

    binding.tvGuangchang.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        binding.tvTuijian.getPaint().setFakeBoldText(false);
        binding.tvTuijian.setTextSize(20);
        binding.tvGuangchang.getPaint().setFakeBoldText(true);
        binding.tvGuangchang.setTextSize(24);
        binding.tvRebang.getPaint().setFakeBoldText(false);
        binding.tvRebang.setTextSize(20);
      }
    });

    binding.tvRebang.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        binding.tvTuijian.getPaint().setFakeBoldText(false);
        binding.tvTuijian.setTextSize(20);
        binding.tvGuangchang.getPaint().setFakeBoldText(false);
        binding.tvGuangchang.setTextSize(20);
        binding.tvRebang.getPaint().setFakeBoldText(true);
        binding.tvRebang.setTextSize(24);
      }
    });

    binding.imgbtnSearch.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Toast.makeText(getActivity(), "搜索", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(getActivity(), SearchActivity.class);
        startActivity(intent);
      }
    });

    binding.imgbtnRelease.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Toast.makeText(getActivity(), "添加", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(getActivity(), PromulgateActivity.class);
        startActivity(intent);
      }
    });
  }
}